package com.goldgov.information.web;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.goldgov.addressbook.service.AddressBookGroupQuery;
import com.goldgov.information.service.Article;
import com.goldgov.information.service.ArticleService;
import com.goldgov.kcloud.core.json.JsonObject;
import com.goldgov.kcloud.core.json.JsonSuccessObject;
import com.goldgov.utils.DateUtil;
import com.goldgov.utils.PropertyUtil;
import com.goldgov.utils.UrlUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/** 
 * @ClassName:InformationController
 * @Description: 信息采集
 * @author MaN
 * @date 2018年7月11日 
 */

@RestController
@RequestMapping("/workbench/open/information")
@Api("信息发布接口")
public class InformationOpenController {

	@Autowired
	private ArticleService articleService;
	
	protected final Log logger = LogFactory.getLog(this.getClass());
	
	@GetMapping("/previewArticleByID")
	@ApiOperation(value = "文章详情", notes ="")
	public JsonSuccessObject<Article> previewArticleByID(@ApiParam(value = "articleID", required = true) @RequestParam("articleID") String articleID,HttpServletRequest request){
		Article obj=articleService.getArticleByID(articleID);
		if (PropertyUtil.objectNotEmpty(obj.getPublishDate())) {
			obj.setPublishDateStr(DateUtil.dateToStr(obj.getPublishDate()));
		}
		JsonSuccessObject<Article> returnResult = new JsonSuccessObject<Article>(obj);
		return returnResult;
	}
	
	@PostMapping("/publishArticles")
	@ApiOperation(value = "发布文章到cms", notes ="")
	public JsonObject<Object> publishArticles(@ApiParam(value = "articleIDs", required = true) @RequestParam("ids") String[] ids,HttpServletRequest request) throws IOException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(null!=ids && ids.length>0) {
			for(int i=0;i<ids.length;i++) {
				Article obj=articleService.getArticleByID(ids[i]);
				obj.setPublishState(AddressBookGroupQuery.ACTIVESTATE_YES);
				obj.setPublishDate(new Date());
				articleService.updateArticle(obj);
				Map<String,String> params=new HashMap<>();
				String articleAuthor= obj.getArticleAuthor();
				if(null!=articleAuthor && !"".equals(articleAuthor)) {
					params.put("articleAuthor", articleAuthor);
				}
				String articleContent= obj.getArticleContent();
				if(null!=articleContent && !"".equals(articleContent)) {
					params.put("articleContent", articleContent);
				}
				String articleSource= obj.getArticleSource();
				if(null!=articleSource && !"".equals(articleSource)) {
					params.put("articleSource", articleSource);
				}
				Integer basicSort= obj.getArticleSort();
				if(null!=basicSort) {
					params.put("basicSort", basicSort.toString());
				}
				String articleKeyword= obj.getArticleKeyWord();
				if(null!=articleKeyword && !"".equals(articleKeyword)) {
					params.put("articleKeyword", articleKeyword);
				}
				String articleTitle= obj.getArticleTitle();
				if(null!=articleTitle && !"".equals(articleTitle)) {
					params.put("basicTitle", articleTitle);
				}
				if(null!=obj.getPublishDateText() && !"".equals(obj.getPublishDateText())) {
					params.put("articlePublishDateText", obj.getPublishDateText());
				}
				if(null!=obj.getArticleUrl() && !"".equals(obj.getArticleUrl())) {
					params.put("articleSite", obj.getArticleUrl());
				}
				params.put("basicDateTime",obj.getPublishDate()==null?sdf.format(new Date()):sdf.format(obj.getPublishDate()));
				params.put("basicCategoryId", obj.getCmsCategoryID()==null?"154":obj.getCmsCategoryID().toString());
				params.put("articleStick", obj.getStickState()==null?"2":obj.getStickState().toString());
				params.put("articleAttribute", obj.getArticleAttribute().toString());
				params.put("articleStatus", Article.PUBLISH_STATE_FABU.toString());
				params.put("isGrab", Article.ARTICLE_IS_GRAB.toString());
				Connection conn = Jsoup.connect(UrlUtil.CMS_ARTICLE_URL).ignoreContentType(true);
				conn.data(params);
				conn.post();
			}
		}
		return JsonSuccessObject.SUCCESS;
	}
}